package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dao.BooksDao;
import com.example.dao.CommentsDao;
import com.example.dao.UsersDao;
import com.example.domain.Books;
import com.example.domain.Comments;
import com.example.domain.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CommentService {

    @Autowired
    private CommentsDao commentsDao;
    @Autowired
    private BooksDao booksDao;
    @Autowired
    private UsersDao usersDao;

//    根据bookId查询评论
    public List<Comments> selectCommentById(Long bookId){
        QueryWrapper<Comments> qw = new QueryWrapper<>();
        qw.eq("bookId",bookId);
        return commentsDao.selectList(qw);
    }

    /*
    根据userId查找comments表的数据，然后再根据查到的表的bookId去查books表，来获取图片书名，最后将每条数据存在map中，最后存在list集合里面
     */
    public List<Map<String, Object>> selectCommentByUserId(Long userId){
        QueryWrapper<Comments> qw = new QueryWrapper<>();
        qw.eq("userId",userId);
        List<Comments> comments = commentsDao.selectList(qw);
        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
        for(Comments co : comments){
            Map<String,Object> map = new HashMap<>();
            map.put("id",co.getId());
            map.put("content",co.getContent());
            map.put("time",co.getTime());
            Books books = booksDao.selectById(co.getBookId());
            map.put("photo",books.getPhoto());
            map.put("name",books.getBookName());
            list.add(map);
        }
        return list;
    }

    public void deleteCommentById(Long id){
        commentsDao.deleteById(id);
    }

    public void addComment(Long userId, Long bookId, String content){
        Comments comments = new Comments();
        comments.setContent(content);
        comments.setTime(new Date());
        comments.setUserId(userId);
        comments.setBookId(bookId);
        commentsDao.insert(comments);
    }

    /*
    根据userId查找comments表的数据，然后再根据查到的表的bookId去查books表，来获取图片书名，最后将每条数据存在map中，最后存在list集合里面
     */
    public List<Map<String, Object>> selectCommentByBookId(Long bookId){
        QueryWrapper<Comments> qw = new QueryWrapper<>();
        qw.eq("bookId",bookId);
        List<Comments> comments = commentsDao.selectList(qw);
        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
        for(Comments co : comments){
            Map<String,Object> map = new HashMap<>();
            map.put("id",co.getId());
            map.put("content",co.getContent());
            map.put("time",co.getTime());
            Users user = usersDao.selectById(co.getUserId());
            map.put("name",user.getUsername());
            Books book = booksDao.selectById(co.getBookId());
            map.put("bookName",book.getBookName());
            map.put("bookId",book.getId());
            list.add(map);
        }
        return list;
    }

    public void delCommentById(Long id){
        commentsDao.deleteById(id);
    }
}
